package com.hospital.insurancepeople.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.api.RemoteEndesctryptionService;
import com.hospital.common.core.module.entity.NFC.NFCResponse;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.insurancepeople.async.WxPayFunc;
import com.hospital.insurancepeople.module.mapper.InsuranceLogsMapper;
import com.hospital.insurancepeople.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Yeman
 * @Date: 2023-04-03-14:54
 * @Description:
 */
@Slf4j
abstract class AbstractMessageInfo<T extends Tip> implements MessageInfo {
    private Tip tip ;
    private WxPayFunc wxPayFunc = SpringContextHolder.getBean(WxPayFunc.class);
    private InsuranceLogsMapper insuranceLogsMapper = SpringContextHolder.getBean(InsuranceLogsMapper.class);

    private RestTemplate restTemplate = SpringContextHolder.getBean(RestTemplate.class);

    private RemoteEndesctryptionService remoteEndesctryptionService = SpringContextHolder.getBean(RemoteEndesctryptionService.class);

    @Override
    public NFCResponse getHisInfo(Tip requestVal, String tableName, String action) {
        String printLog = null;
        String endesUrl = insuranceLogsMapper.getEncryptionUrl();
        NFCResponse nfcResponse = new NFCResponse();
        nfcResponse.setEncData("");
        nfcResponse.setAppId("1G00VOLSP0143F60C80A00005F9F40EF");
        nfcResponse.setChannel("PeopM");
        nfcResponse.setFromApp("HTSoftWare");
        nfcResponse.setToApp("PEOPLEM");
        nfcResponse.setDataType("ENC");
        Object data = requestVal.getData();
        JSONObject jsonObjectInput = JSON.parseObject(data.toString());
        printLog = "\r\n传输数据:" + jsonObjectInput.toJSONString() + "\r\ntableName:" + tableName +
                "\r\naction:" + action +
                "\r\nupload_date:" + DateUtil.getTime();
        tip = getUpDetail(jsonObjectInput);
        if (tip.getCode()==0) {
            Object input = tip.getData();
            nfcResponse.setData(input);
            nfcResponse.setSignType("SM3");
            printLog = printLog + "\r\n人保加密接口入参：" + JSON.toJSONString(nfcResponse);
            Tip descTip = JSONObject.parseObject(remoteEndesctryptionService.getEncDesc(nfcResponse),Tip.class);
            if (descTip.getCode()==200) {
                String enc = JSONObject.toJSONString(descTip.getData());
                printLog = printLog + "\r\n人保加密接口出参、人保推送入参：" + enc + "\r\n推送描述:加密执行成功!"  ;
                if (enc != null) {
                    wxPayFunc.wxPay(enc, tip.getMessage(), printLog,restTemplate);
                }
            } else {
                printLog = printLog + "\r\n推送描述:加密执行失败!" + descTip.getMessage();
                log.info(printLog);
            }
        }else {
        }
        return nfcResponse;
    }

    /**
     * @param requestVal:
     * @description:组装不同的input内容
     * @author: lvyao
     * @date: 2023/4/3 15:15
     * @return: T
     **/
    abstract T getUpDetail(JSONObject requestVal);
}
